//
//  HitTestViewController.m
//  Camera
//
//  Created by gml on 2018/10/16.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "HitTestViewController.h"

#import "HitTestTableView.h"
#import "ATableViewCell.h"
#import "BTableViewCell.h"
#import "CTableViewCell.h"

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface HitTestViewController ()<MKMapViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) MKMapView *mapView;
@property (nonatomic,strong) HitTestTableView *tableView;
@property (nonatomic,strong) NSArray *list;
@property (nonatomic, assign) NSInteger lastcontentOffset; //添加此属性的作用，根据差值，判断ScrollView是上滑还是下拉
@property (nonatomic,strong) UIButton *bottomBtn;


@end
static NSString *ATableViewCellID = @"ATableViewCell";
static NSString *BTableViewCellID = @"BTableViewCell";
static NSString *CTableViewCellID = @"CTableViewCell";

@implementation HitTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.bounds.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-self.navigationController.navigationBar.bounds.size.height)];
    self.mapView.delegate = self;
    self.mapView.mapType = MKMapTypeStandard;
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    //CLLocationCoordinate2DMake：参数： 维度、经度、南北方宽度（km）、东西方宽度（km）
    double lat = 39.907728;
    double lon = 116.397968;
    [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(lat , lon), 500, 500)
                   animated:YES];
    [self.view addSubview:self.mapView] ;
    [self.view addSubview:self.tableView];
//    [self.view addSubview:self.bottomBtn];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ATableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:ATableViewCellID];
    [aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    BTableViewCell *bCell = [tableView dequeueReusableCellWithIdentifier:BTableViewCellID];
    [bCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    CTableViewCell *cCell = [tableView dequeueReusableCellWithIdentifier:CTableViewCellID];
    [cCell setSelectionStyle:UITableViewCellSelectionStyleNone];

    Class cellClass = NSClassFromString(_list[indexPath.row]);
    __weak __typeof(&*self)weakSelf = self;
    if ([cellClass isEqual:[ATableViewCell class]]) {
        
        return aCell;
    }else if ([cellClass isEqual:[BTableViewCell class]]){
        bCell.block = ^(NSInteger index) {
            if (index == 100) {
                [weakSelf action1];
            }else if (index == 101){
                [weakSelf action2];
            }
        };
        return bCell;
    }else{
        return cCell;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Class cellClass = NSClassFromString(_list[indexPath.row]);
    if ([cellClass isEqual:[ATableViewCell class]]) {
        
        return 60.0;
    }else if ([cellClass isEqual:[BTableViewCell class]]){
        return 100.0;
    }else{
        return 200.0;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableView) {
        self.tableView.backgroundColor = [UIColor colorWithWhite:1 alpha:(1 - (scrollView.contentOffset.y*-1/300))];
    }
    CGFloat contentOffset = scrollView.contentOffset.y;
    self.lastcontentOffset = contentOffset;


}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    double lat = 39.907728;
    double lon = 116.397968;
    CGFloat scale = fabs( 500.0 / scrollView.contentOffset.y);
    if (scale > 5) {
        scale = 5;
    }
    NSLog(@"  scale = %f",scale);
    CLLocationCoordinate2D centerCoordinate = [self.mapView convertPoint:self.mapView.center toCoordinateFromView:self.mapView];
    [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(centerCoordinate, lat * scale, lon* scale) animated:YES];
//    [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate animated:YES];
     if (scrollView.contentOffset.y >self.lastcontentOffset) {
         NSLog(@"向下");
        //  向下
        
    }else{
        NSLog(@"向上");
        //  向上
        
    }

}


- (void)action1
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Just Test" preferredStyle:UIAlertControllerStyleAlert];
    // 2.创建并添加按钮
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"OK");
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Cancel");
    }];
    
    [alertController addAction:okAction];           // A
    [alertController addAction:cancelAction];       // B
    [self presentViewController:alertController animated:YES completion:nil];
}
- (void)action2
{
    dispatch_async(dispatch_get_main_queue(), ^{
        //        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNumber]]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",@"10000"]] options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:^(BOOL success) {
            
        }];
    });
}

- (HitTestTableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[HitTestTableView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.bounds.size.height, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-self.navigationController.navigationBar.bounds.size.height) style:UITableViewStylePlain];
//        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ATableViewCell class]) bundle:nil] forCellReuseIdentifier:ATableViewCellID];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BTableViewCell class]) bundle:nil] forCellReuseIdentifier:BTableViewCellID];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CTableViewCell class]) bundle:nil] forCellReuseIdentifier:CTableViewCellID];
        [_tableView setContentInset:UIEdgeInsetsMake(500, 0, self.bottomBtn.bounds.size.height, 0)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = 0;
        _tableView.sectionHeaderHeight = 0;
        _tableView.estimatedRowHeight = 40;
        [_tableView setTableFooterView:self.bottomBtn];
    }
    return _tableView;
}

- (NSArray *)list
{
    if (_list == nil) {
        _list = @[NSStringFromClass([ATableViewCell class]),NSStringFromClass([BTableViewCell class])];
    }
    return _list;
}
- (UIButton *)bottomBtn
{
    if (_bottomBtn == nil) {
        _bottomBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50)];
        _bottomBtn.backgroundColor = [UIColor redColor];
        
    }
    return _bottomBtn;
}



@end
